/**
 * 城市头部的封装
 * @author hongwei 2018-09-07
 */
(function (window) {
    'use strict';
    var cityHeader = function(params) {
        var that = this;
        this.extend(that.params, params);

        that.initHeader();      //step1:初始化头部
        that.archiveBtn();      //step2:点击城市确定
    };
    cityHeader.prototype = {
        //参数
        params : {
            cityOn          : '' //城市类型,single是单城市,multiply是多城市
            ,frameName      : ''   //框架名称：'select_city_many'
            ,firstStorage   : ''                        //第一个storage(必传)，火车：train_start_site
            ,secondStorage  : ''                        //第二个storage（必传），火车：train_end_site
            ,eventName      : 'cityName'   //事件名称，火车：cityName
        }

        //step1:初始化头部
        ,initHeader : function () {
            var that = this;

            initHeader(that.params.frameName,{
                city_on: parseInt(that.params.cityOn)
            });
        }

        //step2:点击城市确定
        ,archiveBtn : function () {
            var that = this;
            $('#achieve').click(function(){
                if(!that.validateConfirm()){
                    return false;
                }
                var cName = '';
                if (parseInt(that.params.cityOn) === 0) {
                    cName = $api.getStorage(that.params.firstStorage);
                } else {
                    cName = $api.getStorage(that.params.secondStorage);
                }
                api.sendEvent({
                    name: that.params.eventName,
                    extra: {
                        city_on: that.params.cityOn,
                        cname:cName
                    }
                });
                api.closeWin();
            });
        }

        //step3:验证确认城市
        , validateConfirm: function (){
            var that = this;
            //step1:所有城市
            var allCityArray = $api.getStorage('all_cities');
            if (parseInt(that.params.cityOn) === 0) {
                //step2:选择的城市
                var firstStorage = that.params.firstStorage;
                var cityNameFirst = $api.getStorage(firstStorage);
                if (cityNameFirst === '') {
                    $toast('数据错误，请重新选择!');
                    window.location.reload();
                }
                cityNameFirst = cityNameFirst.split(' ');
                //step3:所选城市是否在所有城市中
                var judgeFirst = that.isContained(allCityArray, cityNameFirst);
                if (judgeFirst === false) {
                    alert('你选择的城市不在城市列表中！');
                    return false;
                }
            } else {
                //step1:选择的城市
                var secondStorage = that.params.secondStorage;
                var cityNameSecond = $api.getStorage(that.params.secondStorage);
                if (cityNameSecond === '') {
                    $toast('数据错误，请重新选择!');
                    window.location.reload();
                }
                cityNameSecond = cityNameSecond.split(' ');
                //step3:所选城市是否在所有城市中
                var judgeSecond = that.isContained(allCityArray, cityNameSecond);
                if (judgeSecond === false) {
                    alert('你选择的城市不在城市列表中！');
                    return false;
                }
            }
            return true;
        }

        /**
         * 工具1:函数:扩展
         * @param a
         * @param b
         * @returns {*}
         */
        ,extend: function (a, b) {
            for (var key in b) {
                if (b.hasOwnProperty(key)) {
                    a[key] = b[key];
                }
            }
            return a;
        }

        /**
         * 工具2：js判断一个数组是否包含另一个数组
         * @author hongwei 2018-09-07
         * @param aa 例如：[1,2,5,7,3]
         * @param bb 例如：[1,7,2]
         * @returns {boolean}
         */
        ,isContained :function(aa,bb) {
            if (!(aa instanceof Array) || !(bb instanceof Array) || ((aa.length < bb.length))) {
                return false;
            }
            var aaStr = aa.toString();
            for (var i = 0; i < bb.length; i++) {
                if (aaStr.indexOf(bb[i]) < 0) return false;
            }
            return true;
        }

    };
    window.cityHeader =  cityHeader;
})(window);